define(['city', 'form', 'date', 'jcrop'], function (require, exports, module) {
    exports.init = function () {
        $("#birthday").DateSelector({
            ctlYearId: 'birthday-y',
            ctlMonthId: 'birthday-m',
            ctlDayId: 'birthday-d',
            showNull: true,
            minYear: new Date().getFullYear() - 20,
            maxYear: new Date().getFullYear()
        });
        $(function () {
            $('#js-dog-photo-a').on('click', function () {
                $(window.frames['fileUploadIFrame'].document).find('#file').click();
                var tt = setInterval(function () {
                    var fileVal = $(window.frames['fileUploadIFrame'].document).find('#file').val();
                    if (fileVal && fileVal.length) {
                        clearInterval(tt);
                        $(window.frames['fileUploadIFrame'].document).find('form').submit();
                        var obj = $('#js-dog-edit-photo').clone();
                        obj.addClass('js-show-edit');
                        obj.modal();
                        obj.on('hide.bs.modal', function () {
                            obj.remove();
                        });
                    }
                }, 300);
            });
        });
        window.uploadCallBack = function(obj) {
            setTimeout(function () {
                if (obj.fileName) {
                    var src = '/' + obj.fileName;
                    var img = new Image();
                    img.onload = function () {
                        var imgObj = $('.js-show-edit').find('.js-edit-src');
                        imgObj.attr('src', src);
                        imgObj.Jcrop({
                            aspectRatio: 1,
                            setSelect: [0, 0, 300, 300],
                            onSelect: function (c) {
                                $('#x').val(c.x);
                                $('#y').val(c.y);
                                $('#w').val(c.w);
                                $('#h').val(c.h);
                                $('#width').val($('.js-show-edit').find('.js-edit-src').width());
                            }
                        });
                        $('#js-coords').find('input[name="file"]').val(obj.fileName);
                    }
                    img.src = src;
                } else {
                    alert(obj.error);
                }
            }, 200);
        }

        function checkCoords(obj) {
            var x = $('#x').val();
            var y = $('#y').val();
            var w = $('#w').val();
            var h = $('#h').val();
            var width = $('#width').val();
            var str = 'x->' + x + ' y->' + y + ' w->' + w + ' h->' + h + ' width->' + width;
            if (!parseInt($('#w').val())) {
                alert('请先选择要裁剪的区域后，再提交。');
                return false;
            }
            ;
            $(obj).ajaxSubmit({
                url: '/Ajax/File/coords?stmp=' + new Date().toString(),
                type: 'post',
                dataType: 'json',

                success: function (d) {
                    if (d.error) {
                        alert(d.data);
                    } else {
                        var src = d.data[0];
                        var file = d.data[1];
                        $('#js-dog-photo').attr('src', src);
                        $('input[name="photo"]').val(file);
                    }
                    $('.js-show-edit').modal('hide');
                }
            });
            return false;
        }

        $('#js-coords').on('submit', function () {
            return checkCoords(this);
        });
        require.async('breed', function(a) {
            a.init($('#js-input-breed'));
        });
        $('#city').city({
            code: $('input[name="city"]').val(),
            callback: function (code, name) {
                $('input[name="city"]').val(code);
            }
        });
    }
});